#pragma once
#include "DXAMLCommon.h"
#include "EncodedPtr.h"
#include "IPlatformServices.h"



extern EncodedPtr<IPlatformServices> gps;

template <typename T>
class CXcpDList
{
public: 
	struct XCPDListNode
	{
	public: 
		T * m_pData;
		XCPDListNode * m_pNext;
		XCPDListNode * m_pPrev;

		XCPDListNode()
		{
			this->m_pData = NULL;
			this->m_pNext = NULL;
			this->m_pPrev = NULL;
		}
	};
public: 
	CXcpDList()
	{
		NOTIMPL
	}

	~CXcpDList()
	{
		NOTIMPL
	}

	void * AddAndGetIndex(T * pData)
	{
		HRESULT hr = S_OK;

		XCPDListNode *pTemp = this->m_pHead;

		XCPDListNode * pNewNode = new XCPDListNode();

		XASSERT_NOTNULL_WITH_HR(pNewNode,0x8007000Eu);

		this->m_pHead = pNewNode;
		this->m_pHead->m_pNext = pTemp;
		this->m_pHead->m_pData = pData;
		if ( pTemp )
			pTemp->m_pPrev = this->m_pHead;

		return pNewNode;
	}

	HRESULT RemoveAt(void * index)
	{
		XCPDListNode *pPrev;
		XCPDListNode *pNext;

		if ( index )
		{
			pNext = static_cast<CXcpDList<T>::XCPDListNode*>(index)->m_pNext;
			pPrev = static_cast<CXcpDList<T>::XCPDListNode*>(index)->m_pPrev;
			if ( pPrev )
				pPrev->m_pNext = pNext;
			if ( pNext )
				pNext->m_pPrev = pPrev;
			if ( static_cast<CXcpDList<T>::XCPDListNode*>(index) == this->m_pHead )
				this->m_pHead = pNext;
			gps->Delete(index);

		}
		return S_OK;
	}

	HRESULT Clean()
	{
		while(this->m_pHead)
		{
			XCPDListNode * pTemp = this->m_pHead;
			this->m_pHead->m_pData = NULL;
			gps->Delete(this->m_pHead);
			this->m_pHead = pTemp;
		}

		this->m_pHead = NULL;
	}
	XCPDListNode * GetHead()
	{
		NOTIMPL
	}
private: 
	XCPDListNode * m_pHead;
};